package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> query(CourseDto courseDto) {
       //分页
        PageHelper.startPage(courseDto.getPage(), courseDto.getPageSize());
        //查询数据
        List<Course> courses = courseMapper.selectCourse(courseDto);
        Page<Course> page = (Page<Course>) courses;
        return new PageResult<>(page.getTotal(), page.getResult());
    }
   /**
   * 删除课程
   */
    @Override
    public void delete(Integer id) {
        courseMapper.deleteById(id);
    }
    /**
     * 添加课程
     */
    @Override
    public void add(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.add(course);
    }
    /**
     * 查询课程根据ID
     */
    @Override
    public Course selectById(Integer id) {
        return courseMapper.selectId(id);
    }
    /**
     * 修改课程
     */
    @Override
    public void updata(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updataCourse(course);
    }
    /**
     * 查询所有课程
     */
    @Override
    public List<Course> findAll() {
        return courseMapper.selectAll();
    }
    /**
     * 根据学科类别查询课程
     */
    @Override
    public List<Course> findBySubject(Integer subject) {
        return courseMapper.selcetSubject(subject);
    }
}

